package 每日一题OJ;

import java.util.*;

/**
 * @author shy_black
 * @date 2019/3/28 22:52
 * @Description:
 * 1.检查root是否为空，空则返回true
 * 2.判断二叉树左右子树绝对值是否大于1，如大于1，返回false
 * 3.向下递归，进行循环操作
 */
public class 二叉树平衡检查 {
}


class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

    public boolean isBalance(TreeNode root) {
        // write code here
        if (root == null)
            return true;
        if (Math.abs(high(root.left) - high(root.right)) > 1) {
            return false;
        }
        return isBalance(root.left) && isBalance(root.right);
    }

    public static int high(TreeNode node) {
        if (node == null) {
            return 0;
        }
        return Math.max(high(node.left), high(node.right)) + 1;
    }
}